import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import path from 'node:path'
import dts from 'vite-plugin-dts'
export default defineConfig(({ command }) => {
  const isDev = command === 'serve'
  return {
    plugins: [react(), dts({
      outDir: path.resolve(__dirname, 'dist'),
      insertTypesEntry: true, // /这是dts插件的配置，用于生成类型文件
      include: ['packages/**/*.ts', 'packages/**/*.tsx'],
      rollupTypes: true
    })],
    root: isDev ? path.resolve(__dirname, 'example') : undefined,
    server: {
      port: 3000,
      open: true,
    },
    build: {
      outDir: path.resolve(__dirname, 'dist'),
      lib: {
        entry: path.resolve(__dirname, 'packages/index.ts'),
        name: 'leeui',
        formats: ['es', 'umd', 'cjs', 'iife'],
        fileName: (format) => `leeui.${format}.js`,
      },
      emptyOutDir: false,
      rollupOptions: {
        // 确保外部化处理那些你不想打包进库的依赖
        external: ['react', 'react-dom'],
        output: {
          // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
          globals: {
            react: 'React',
            'react-dom': 'ReactDOM',
          },
        },
      },
    }
  }
})